package org.example.managere.service.impl;

import jakarta.persistence.EntityNotFoundException;
import org.example.managere.dto.OperationDto;
import org.example.managere.entity.operationdata.Operation;
import org.example.managere.repository.operationdata.OperationRepository;
import org.example.managere.service.OperationService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OperationServiceImpl implements OperationService {

    private final OperationRepository operationRepository;
    private final ModelMapper modelMapper;

    @Autowired
    public OperationServiceImpl(OperationRepository operationRepository, ModelMapper modelMapper) {
        this.operationRepository = operationRepository;
        this.modelMapper = modelMapper;
    }

    @Override
    public List<OperationDto> getAllOperations() {
        return operationRepository.findAll().stream()
                .map(operation -> modelMapper.map(operation, OperationDto.class))
                .collect(Collectors.toList());
    }

    @Override
    public Page<OperationDto> getOperationsWithPagination(Pageable pageable) {
        Page<Operation> operationsPage = operationRepository.findAll(pageable);
        
        // 打印从数据库获取的原始数据
        operationsPage.getContent().forEach(operation -> {
            System.out.println("Operation ID: " + operation.getId());
            System.out.println("Name: " + operation.getName());
            System.out.println("System: " + operation.getSystem());
            System.out.println("Subsystem: " + operation.getSubsystem());
            System.out.println("FMode: " + operation.getFMode());
            System.out.println("FCause: " + operation.getFCause());
            System.out.println("FEffects: " + operation.getFEffects());
            System.out.println("SingleFailureRate: " + operation.getSingleFailureRate());
            System.out.println("HazardRating: " + operation.getHazardRating());
            System.out.println("CreateTime: " + operation.getCreateTime());
            System.out.println("UpdateTime: " + operation.getUpdateTime());
            System.out.println("------------------------------------");
        });
        
        return operationsPage.map(operation -> {
            OperationDto dto = modelMapper.map(operation, OperationDto.class);
            // 打印映射后的DTO数据
            System.out.println("DTO ID: " + dto.getId());
            System.out.println("DTO Name: " + dto.getName());
            System.out.println("DTO System: " + dto.getSystem());
            System.out.println("DTO Subsystem: " + dto.getSubsystem());
            System.out.println("DTO FMode: " + dto.getFMode());
            System.out.println("DTO FCause: " + dto.getFCause());
            System.out.println("DTO FEffects: " + dto.getFEffects());
            System.out.println("DTO SingleFailureRate: " + dto.getSingleFailureRate());
            System.out.println("DTO HazardRating: " + dto.getHazardRating());
            System.out.println("DTO CreateTime: " + dto.getCreateTime());
            System.out.println("DTO UpdateTime: " + dto.getUpdateTime());
            System.out.println("====================================");
            return dto;
        });
    }

    @Override
    public OperationDto getOperationByName(String name) {
        try {
            Integer id = Integer.valueOf(name);
            return getOperationById(id);
        } catch (NumberFormatException e) {
            throw new EntityNotFoundException("无效的ID: " + name);
        }
    }
    
    @Override
    public OperationDto getOperationById(Integer id) {
        Operation operation = operationRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("未找到ID为" + id + "的操作记录"));
        return modelMapper.map(operation, OperationDto.class);
    }

    @Override
    public List<OperationDto> findBySystemContaining(String system) {
        return operationRepository.findBySystemContaining(system).stream()
                .map(operation -> modelMapper.map(operation, OperationDto.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<OperationDto> findBySubsystemContaining(String subsystem) {
        return operationRepository.findBySubsystemContaining(subsystem).stream()
                .map(operation -> modelMapper.map(operation, OperationDto.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<OperationDto> findByFModeContaining(String fMode) {
        return operationRepository.findByfModeContaining(fMode).stream()
                .map(operation -> modelMapper.map(operation, OperationDto.class))
                .collect(Collectors.toList());
    }

    @Override
    public OperationDto createOperation(OperationDto operationDto) {
        Operation operation = modelMapper.map(operationDto, Operation.class);
        Operation savedOperation = operationRepository.save(operation);
        return modelMapper.map(savedOperation, OperationDto.class);
    }

    @Override
    public OperationDto updateOperation(String name, OperationDto operationDto) {
        try {
            Integer id = Integer.valueOf(name);
            return updateOperation(id, operationDto);
        } catch (NumberFormatException e) {
            throw new EntityNotFoundException("无效的ID: " + name);
        }
    }
    
    @Override
    public OperationDto updateOperation(Integer id, OperationDto operationDto) {
        if (!operationRepository.existsById(id)) {
            throw new EntityNotFoundException("未找到ID为" + id + "的操作记录");
        }
        
        operationDto.setId(id); // 确保ID正确设置
        Operation operation = modelMapper.map(operationDto, Operation.class);
        Operation updatedOperation = operationRepository.save(operation);
        return modelMapper.map(updatedOperation, OperationDto.class);
    }

    @Override
    public void deleteOperation(String name) {
        try {
            Integer id = Integer.valueOf(name);
            deleteOperation(id);
        } catch (NumberFormatException e) {
            throw new EntityNotFoundException("无效的ID: " + name);
        }
    }
    
    @Override
    public void deleteOperation(Integer id) {
        if (!operationRepository.existsById(id)) {
            throw new EntityNotFoundException("未找到ID为" + id + "的操作记录");
        }
        operationRepository.deleteById(id);
    }
} 